-- spell_bonus2
-- created by zhuangsl
-- 进入下一层时，获得特定魔法卷轴

return {
    apply = function(prop, baseValue)
        return baseValue + prop[3];
    end,

    -- 进入下一层触发
    trigger = function(who, prop, arg)
        if who.type ~= OBJECT_TYPE_USER then
            return false;
        end

        -- 客户端此时还在前一层，需要+1计算
        local floor = DungeonM.currentLayer() + 1;
        -- 卷轴的数量和阶位
        local count = prop[3];
        local spell = tonumber(prop[2]);
        local rank  = SpellM.query(spell, "rank");
        
        -- 每N层给一张，1~3阶的N等于阶位； 4阶的5层，5阶的20层
        local bonusFloor = math.max(rank, 1);
        if rank == 4 or spell == 4055 then
            bonusFloor = 5;
        elseif rank >= 5 then
            bonusFloor = 20;
        end

        -- 不在对应楼层不给
        if floor % bonusFloor ~= 0 then
            return false;
        end

        -- 高阶卷轴，不是第一次达到的楼层不给
        local max_dungeon_layer = ME.user.dbase:query("max_dungeon_layer", 0);
        if rank >= 5 and max_dungeon_layer + 1 > floor then
            return false;
        end 

        BonusM.doBonus({ 1, spell, count, }, "spell_bonus2");

        return true;
    end,
    
    desc = function(prop)
        local row = PropM.getRow(prop[1]);
        local desc = row.desc;

        -- 计算楼层
        local rank  = SpellM.query(prop[2], "rank");
        -- 每N层给一张，1~3阶的N等于阶位； 4阶的5层，5阶的20层
        local bonusFloor;
        if rank <= 1 then
            bonusFloor = "";
        elseif rank == 4 then
            bonusFloor = 5;
        elseif rank >= 5 then
            bonusFloor = 20;
        else
            bonusFloor = rank;
        end
        desc = string.gsub(desc, "{floor}", bonusFloor);
        
        -- 数量
        desc = string.gsub(desc, "{1}", prop[3]);
        
        -- 物品名称
        desc = string.gsub(desc, "{0}", ItemM.query(prop[2],"name"));

        return desc;
    end,
    
    record = function(target, prop, path)
        return false;
    end
};